﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QuanLyHocPhi.BLL;
using QuanLyHocPhi.Model;

namespace QuanLyHocPhi.GUI.DuLieu
{
    public partial class SelectLopSinhHoatDialog : QuanLyHocPhi.GUI.FormBaseDialog
    {
        private Truong SelectedTruong
        {
            get
            {
                if (cbxTruong.SelectedItem == null)
                    return null;
                else
                    return (cbxTruong.SelectedItem as Truong);
            }
        }

        public Lop SelectedLop
        {
            get
            {
                if (bsLop.Current == null)
                    return null;
                else
                    return bsLop.Current as Lop;
            }
        }

        private string SelectedNamTuyenSinh
        {
            get
            {
                if (cbxNamTuyenSinh.SelectedItem == null)
                    return null;
                else
                    return cbxNamTuyenSinh.SelectedItem.ToString();
            }
        }

        private bool m_IsChuyen = false;
        public SelectLopSinhHoatDialog(Lop lop,bool chuyen=false)
        {
            InitializeComponent();
            m_IsChuyen = chuyen;
            // Bind dữ liệu
            gridLop.AutoGenerateColumns = false;
            gridLop.DataSource = bsLop;
            colLop_TenLop.DataPropertyName = "TenLop";

            // Nạp dữ liệu
            this.LockUpdate();
            // Nạp danh sách trường
            IList<Truong> lstTruong;
            if (m_IsChuyen)
            {
                lstTruong = new List<Truong>();
                lstTruong.Add(BizTruong.GetTruongByMaTruong("GDTC"));
                cbxTruong.Enabled = false;
            }
            else
                lstTruong = BizTruong.GetList_Truong();
            cbxTruong.DisplayMember = "TenTruong";
            cbxTruong.DataSource = lstTruong;

            // Nạp danh sách lớp của trường đang chọn (nếu có)
            Truong truong = lop != null ? BizTruong.GetTruongByMaTruong(lop.MaTruong) : null;
            string namTuyenSinh = lop != null ? lop.NamTuyenSinh.ToString() : null;
            if (truong == null)
            {
                cbxTruong.Enabled = true;
            }
            else
            {
                for (int i = 0; i < lstTruong.Count; i++)
                    if (lstTruong[i].MaTruong == truong.MaTruong)
                    {
                        cbxTruong.SelectedIndex = i;
                        cbxTruong.Enabled = false;
                        break;
                    }
            }
            NapDanhSachNamTuyenSinh(truong, namTuyenSinh);
            NapDanhSachLop(truong, namTuyenSinh ,lop);
            this.UnlockUpdate();
        }

        private void NapDanhSachLop(Truong truong, string namTuyenSinh, Lop lopToFocus)
        {
            if (truong == null || namTuyenSinh.IsNullOrEmpty())
                bsLop.DataSource = null;
            else
            {
                int nam = -1;
                if (int.TryParse(namTuyenSinh, out nam))
                {
                    var lstLop = BizTruong.GetList_LopKhongChuyen(truong.MaTruong, nam);
                    bsLop.DataSource = lstLop;
                    if (lopToFocus != null)
                    { 
                        for (int i = 0; i<lstLop.Count;i++)
                            if (lstLop[i].MaLop == lopToFocus.MaLop)
                            {
                                bsLop.Position = i;
                                break;
                            }
                    }
                }
                else
                    bsLop.DataSource = null;
            }
            gridLop.AutoGenerateColumns = false;
            gridLop.DataSource = bsLop;           
        }

        private void NapDanhSachNamTuyenSinh(Truong truong, string namTuyenSinhToFocus)
        {
            if (truong == null)
                cbxNamTuyenSinh.DataSource = null;
            else
                cbxNamTuyenSinh.DataSource = BizTruong.GetList_NamTuyenSinh(truong.MaTruong);
            if (namTuyenSinhToFocus.IsNullOrEmpty() == false)
            {
                for (int i = 0; i < cbxNamTuyenSinh.Items.Count; i++)
                    if (cbxNamTuyenSinh.Items[i].ToString() == namTuyenSinhToFocus)
                    {
                        cbxNamTuyenSinh.SelectedIndex = i;
                        break;
                    }
            }
        }

        private void OnCbxTruongSelectedIndexChanged(object sender, EventArgs e)
        {
            if (m_LockUpdate)
                return;
            NapDanhSachNamTuyenSinh(this.SelectedTruong, this.SelectedNamTuyenSinh);
            NapDanhSachLop(this.SelectedTruong, this.SelectedNamTuyenSinh, this.SelectedLop);
        }

        private void OnBsLopCurrentChanged(object sender, EventArgs e)
        {
            CapNhatTrangThaiLopGoc();
        }

        private void CapNhatTrangThaiLopGoc()
        {
            btnOK.Enabled = (this.SelectedLop != null);
        }

        private void OnCbxNamTuyenSinhSelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.m_LockUpdate)
                return;
            NapDanhSachLop(this.SelectedTruong, this.SelectedNamTuyenSinh, this.SelectedLop);
        }
    }
}
